package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.exception.NegativeNumberException;
import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 充值交易数据明细表实体类 作者: 日期：2018-08-12 17:15:17 描述：
 */
public class TradeRecharge extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 前次交易终端编号
	 */
	private String exDeviceId;

	/*
	 * 前次交易类型
	 */
	private String exTradeCode;

	/*
	 * 前次交易时间
	 */
	private String exTradeTime;

	/*
	 * 前次交易金额
	 */
	private Long exTradeMoney;

	/*
	 * 前次交易操作员号
	 */
	private String exOper;

	/*
	 * 前次充值终端代号
	 */
	private String exRechargedId;

	/*
	 * 前次充值交易类型
	 */
	private String exRechargeTc;

	/*
	 * 前次充值交易时间
	 */
	private String exRechargeTime;

	/*
	 * 前次充值交易金额
	 */
	private Long exRechargeTv;

	/*
	 * 前次充值操作员
	 */
	private String exRechargeOper;

	/*
	 * 前次充值卡序号
	 */
	private Long exRechargeCs;

	/**
	 * 默认构造方法
	 **/
	public TradeRecharge() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getExDeviceId() {
		return exDeviceId;
	}

	public void setExDeviceId(String exDeviceId) {
		this.exDeviceId = exDeviceId;
	}

	public String getExTradeTime() {
		return exTradeTime;
	}

	public void setExTradeTime(String exTradeTime) throws ParseException {
		checkTimeStyle(exTradeTime, "ExTradeTime");
		this.exTradeTime = exTradeTime;
	}

	public Long getExTradeMoney() {
		return exTradeMoney;
	}

	public void setExTradeMoney(Long exTradeMoney) throws NegativeNumberException {
		checkMoneyStyle(exTradeMoney, "ExTradeMoney");
		this.exTradeMoney = exTradeMoney;
	}

	public String getExOper() {
		return exOper;
	}

	public void setExOper(String exOper) {
		this.exOper = exOper;
	}

	public String getExRechargedId() {
		return exRechargedId;
	}

	public void setExRechargedId(String exRechargedId) {
		this.exRechargedId = exRechargedId;
	}

	public String getExRechargeTc() {
		return exRechargeTc;
	}

	public void setExRechargeTc(String exRechargeTc) {
		this.exRechargeTc = exRechargeTc;
	}

	public String getExRechargeTime() {
		return exRechargeTime;
	}

	public void setExRechargeTime(String exRechargeTime) throws ParseException {
		checkTimeStyle(exRechargeTime, "exRechargeTime");
		this.exRechargeTime = exRechargeTime;
	}

	public Long getExRechargeTv() {
		return exRechargeTv;
	}

	public void setExRechargeTv(Long exRechargeTv) {
		this.exRechargeTv = exRechargeTv;
	}

	public String getExTradeCode() {
		return exTradeCode;
	}

	public void setExTradeCode(String exTradeCode) {
		this.exTradeCode = exTradeCode;
	}

	public String getExRechargeOper() {
		return exRechargeOper;
	}

	public void setExRechargeOper(String exRechargeOper) {
		this.exRechargeOper = exRechargeOper;
	}

	public Long getExRechargeCs() {
		return exRechargeCs;
	}

	public void setExRechargeCs(Long exRechargeCs) {
		this.exRechargeCs = exRechargeCs;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeRecharge init(String message) throws Exception {



		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {

			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 14, 10, 8, 8, 8, 5, 10, 9, 2, 14, 8, 6, 20, 10, 2, 2, 16, 1, 1, 9,
					2, 14, 8, 6, 5, 2, 2, 2, 4, 4, 1, 4, 5, -1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);

			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setBalance(Long.parseLong(messageParser.getFiled(11))); // 卡余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(12)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(13)));
			this.setExDeviceId(messageParser.getFiled(14));

			this.setExTradeCode(messageParser.getFiled(15));
			this.setExTradeTime(messageParser.getFiled(16));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(17)));
			this.setExOper(messageParser.getFiled(18));
			this.setSamcardId(messageParser.getFiled(19));

			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(20)));
			this.setCardCode(messageParser.getFiled(21));
			this.setTradeSub(messageParser.getFiled(22));
			this.setPhysicalId(messageParser.getFiled(23));
			this.setTestFlag(messageParser.getFiled(24));
			this.setGreyFlag(messageParser.getFiled(25));

			this.setExRechargedId(messageParser.getFiled(26));
			this.setExRechargeTc(messageParser.getFiled(27));
			this.setExRechargeTime(messageParser.getFiled(28));
			this.setExRechargeTv(Long.parseLong(messageParser.getFiled(29)));
			this.setExRechargeOper(messageParser.getFiled(30));
			this.setExRechargeCs(Long.parseLong(messageParser.getFiled(31)));

			this.setMedia(messageParser.getFiled(32));

			this.setCardVersion(messageParser.getFiled(33));
			this.setSubCardCode(messageParser.getFiled(34));
			this.setCityCode(messageParser.getFiled(35));
			this.setBusinessCode(messageParser.getFiled(36));

			this.setIndustrySplit(messageParser.getFiled(37));
			this.setIssuer(messageParser.getFiled(38));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(39)));
			super.setIndustryData(messageParser.getFiled(40));
			super.setCrc16(messageParser.getFiled(41));

		} else if(message.startsWith("SZMETROV3")) {

			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
			 付费方式
			 支付方式
			 发卡方机构码
			 发卡方交易流水号
			 运营模式
			 三、票卡发行方标识	从长度4变为长度2
			 */
			int[] splitNumber = {
					9, 8, 20, 2, 10,   9, 6, 14, 10, 8,   8, 8, 5, 10, 9,   2, 14, 8, 6, 20,   10, 2, 2, 16, 1,
					1, 9, 2, 14, 8,   6, 5, 2, 2, 2,   4, 4,
					1, 2, 8, 10, 4,    //V3增加的字段
					1, 2, 5,
					-1, 5};
			messageParser = new TradeMessageParser(message, splitNumber);

			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setBalance(Long.parseLong(messageParser.getFiled(11))); // 卡余额
			this.setCardSeq(Long.parseLong(messageParser.getFiled(12)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(13)));
			this.setExDeviceId(messageParser.getFiled(14));

			this.setExTradeCode(messageParser.getFiled(15));
			this.setExTradeTime(messageParser.getFiled(16));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(17)));
			this.setExOper(messageParser.getFiled(18));
			this.setSamcardId(messageParser.getFiled(19));

			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(20)));
			this.setCardCode(messageParser.getFiled(21));
			this.setTradeSub(messageParser.getFiled(22));
			this.setPhysicalId(messageParser.getFiled(23));
			this.setTestFlag(messageParser.getFiled(24));
			this.setGreyFlag(messageParser.getFiled(25));

			this.setExRechargedId(messageParser.getFiled(26));
			this.setExRechargeTc(messageParser.getFiled(27));
			this.setExRechargeTime(messageParser.getFiled(28));
			this.setExRechargeTv(Long.parseLong(messageParser.getFiled(29)));
			this.setExRechargeOper(messageParser.getFiled(30));
			this.setExRechargeCs(Long.parseLong(messageParser.getFiled(31)));

			this.setMedia(messageParser.getFiled(32));

			this.setCardVersion(messageParser.getFiled(33));
			this.setSubCardCode(messageParser.getFiled(34));
			this.setCityCode(messageParser.getFiled(35));
			this.setBusinessCode(messageParser.getFiled(36));


			// V3增加字段
			this.setPayMethod(messageParser.getFiled(37));
			this.setPayType(messageParser.getFiled(38));
			this.setIssuerCode(messageParser.getFiled(39));
			this.setIssuerSeq(messageParser.getFiled(40));
			this.setOperMode(messageParser.getFiled(41));

			this.setIndustrySplit(messageParser.getFiled(42));
			this.setIssuer(messageParser.getFiled(43));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(44)));
			super.setIndustryData(messageParser.getFiled(45));
			super.setCrc16(messageParser.getFiled(46));
		}
		return this;
	}

	@Override
	public String toString() {
		return super.toString() + "\n{\"TradeRecharge\": {\"commId\": \"" + commId + "\", \"exDeviceId\": \""
				+ exDeviceId + "\", \"exTradeCode\": \"" + exTradeCode + "\", \"exTradeTime\": \"" + exTradeTime
				+ "\", \"exTradeMoney\": \"" + exTradeMoney + "\", \"exOper\": \"" + exOper
				+ "\", \"exRechargedId\": \"" + exRechargedId + "\", \"exRechargeTc\": \"" + exRechargeTc
				+ "\", \"exRechargeTime\": \"" + exRechargeTime + "\", \"exRechargeTv\": \"" + exRechargeTv
				+ "\", \"exRechargeOper\": \"" + exRechargeOper + "\", \"exRechargeCs\": \"" + exRechargeCs + "\"}}";
	}

}